//
//  TBCRadioStationViewController.m
//  待定
//
//  Created by EricXie on 15/11/6.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import <MJRefresh.h>
#import "TBCAppTools.h"
#import "TBCRadioStationViewController.h"
#import "TBCHeaderView.h"
#import "TBCStationTableViewCell.h"
#import "TBCRadioListModel.h"
#import "TBCUserModel.h"
#import "TBCPlayerViewController.h"

@interface TBCRadioStationViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TBCHeaderView *headerV;

@property (nonatomic, strong) BaseTableView *myTableV;

@property (nonatomic, strong) NSMutableArray *allRadioArray;

@property (nonatomic, assign) NSInteger dataIndex;

@end

@implementation TBCRadioStationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.allRadioArray = [NSMutableArray array];
    
    self.headerV = [[TBCHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.5)];
    _headerV.radio = _radio;
    

    self.myTableV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    _myTableV.delegate = self;
    _myTableV.dataSource = self;
    _myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableV.tableHeaderView = _headerV;
    [self.view addSubview:_myTableV];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getHeaderData)];
    _myTableV.header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getFooterData)];
    _myTableV.footer = footer;
    
    [_myTableV.header beginRefreshing];
    
    UIImage *image = [UIImage imageNamed:@"fanhui.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction:)];
}

//自定义左侧按钮
-(void)leftBarAction:(UIBarButtonItem *)barItem
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)getHeaderData
{
    self.dataIndex = 0;
    [self refreshData];
}

- (void)getFooterData
{
    self.dataIndex += 10;
    [self getDataWithAFN];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 0.15;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allRadioArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TBCStationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[TBCStationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.radio = [_allRadioArray objectAtIndex:indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TBCPlayerViewController *radioPlayerVC = [TBCPlayerViewController shareRadioPlayerViewController];
    radioPlayerVC.allMusicArray = _allRadioArray;
    radioPlayerVC.indexPath = indexPath.row;
    radioPlayerVC.radio = [_allRadioArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:radioPlayerVC animated:YES];
    
}

-(void)refreshData{
    NSString *url_string = @"http://api2.pianke.me/ting/radio_detail";
    
    NSDictionary *para = @{@"radioid":_radio.radioid,@"client":@"1",@"auth":@"C807FCuwm36lDC8l3HMTslDJxQMKpNLeNoj1vCGMzcWP22NZLTRhY4fI",@"version":@"3.0.6",@"deviceid":@"63900960-1ED5-47D0-B561-86C09F41ED08"};
    
    NSString *cookie = @"PHPSESSID=dlbvjmr0e59672q3lseu1fomu5";
    
    [TBCAppTools postLocalDataWithURL:url_string Parameters:para Cookie:cookie TBCToolsBlock:^(id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSArray *listArray = [[dic objectForKey:@"data"] objectForKey:@"list"];
        for(NSMutableDictionary *dictionary in listArray){
            TBCRadioListModel *radio = [[TBCRadioListModel alloc] init];
            [radio setValuesForKeysWithDictionary:dictionary];
            radio.musicUrl = [[dictionary objectForKey:@"playInfo"] objectForKey:@"musicUrl"];
            NSDictionary *userInfo = [[dictionary objectForKey:@"playInfo"] objectForKey:@"userinfo"];
            radio.userModel = [[TBCUserModel alloc] init];
            [radio.userModel setValuesForKeysWithDictionary:userInfo];
            [_allRadioArray addObject:radio];
        }
        
        [_myTableV.header endRefreshing];
        [_myTableV.footer endRefreshing];
        [_myTableV reloadData];
    }];
    
}

-(void)getDataWithAFN{
    
    NSString *url_string = @"http://api2.pianke.me/ting/radio_detail_list";
    
    NSDictionary *para = @{@"radioid":_radio.radioid,@"start":[NSString stringWithFormat:@"%ld",_dataIndex],@"client":@"1",@"auth":@"C807FCuwm36lDC8l3HMTslDJxQMKpNLeNoj1vCGMzcWP22NZLTRhY4fI",@"limit":@"10",@"version":@"3.0.6",@"deviceid":@"63900960-1ED5-47D0-B561-86C09F41ED08"};
    
    NSString *cookie = @"PHPSESSID=dlbvjmr0e59672q3lseu1fomu5";
    
    [TBCAppTools getDatawithPost:url_string paramenters:para cookie:cookie Block:^(id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSArray *listArray = [[dic objectForKey:@"data"] objectForKey:@"list"];
        for(NSMutableDictionary *dictionary in listArray){
            TBCRadioListModel *radio = [[TBCRadioListModel alloc] init];
            [radio setValuesForKeysWithDictionary:dictionary];
            radio.musicUrl = [[dictionary objectForKey:@"playInfo"] objectForKey:@"musicUrl"];
            NSDictionary *userInfo = [[dictionary objectForKey:@"playInfo"] objectForKey:@"userinfo"];
            radio.userModel = [[TBCUserModel alloc] init];
            [radio.userModel setValuesForKeysWithDictionary:userInfo];
            [_allRadioArray addObject:radio];
        }
        
        [_myTableV.header endRefreshing];
        [_myTableV.footer endRefreshing];
        [_myTableV reloadData];
        
    }];
    
}

@end
